package com.abel.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.abel.web.view.SearchViewBean;

public class SearchViewValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		return SearchViewBean.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {
		
		SearchViewBean viewBean = (SearchViewBean)obj;
		
		if ((viewBean.getLastName() == null
						|| viewBean.getLastName().equals(""))
				&& (viewBean.getSsn() == null
						|| viewBean.getSsn().equals(""))
				&& (viewBean.getDateSubmittedBegin() == null
						|| viewBean.getDateSubmittedBegin().equals(""))
				&& (viewBean.getDateSubmittedEnd() == null
						|| viewBean.getDateSubmittedEnd().equals(""))
				&& (viewBean.getDateLastSevenDays() == null
						|| viewBean.getDateLastSevenDays().equals(""))) {
			
			errors.rejectValue("", "error.null",null,"Atleast one of the search fields has to be entered.  Or the last 7 days option should be selected to search data");
		}
		
		if (((viewBean.getLastName() != null
				&& !viewBean.getLastName().equals(""))   
				|| (viewBean.getSsn() != null
						&& !viewBean.getSsn().equals(""))
				|| (viewBean.getDateSubmittedBegin() != null
						&& !viewBean.getDateSubmittedBegin().equals(""))
				|| (viewBean.getDateSubmittedEnd() != null
						&& !viewBean.getDateSubmittedEnd().equals("")))
				&& viewBean.getDateLastSevenDays() != null) {
			
			errors.rejectValue("", "error.null",null,"Both options cannot be used as the same time.  The last 7 days report may be viewed or use the search fields for specific results");
		}

	}

}
